package com.syh.jn.scm.service.impl.operation.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.other.ExtStringUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.operation.entity.system.PermissionType;
import com.syh.jn.scm.domain.operation.repository.system.PermissionTypeRepository;
import com.syh.jn.scm.dto.operation.system.permission.PerTypePackageDto;
import com.syh.jn.scm.dto.operation.system.permission.PermissionTypeMto;
import com.syh.jn.scm.dto.operation.system.permission.PermissionTypeSto;
import com.syh.jn.scm.dto.operation.system.permission.PermissionTypeVo;
import com.syh.jn.scm.service.api.operation.system.PermissionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class PermissionTypeServiceImpl extends AbstractServiceImpl<PermissionTypeSto, PermissionTypeMto, PermissionTypeVo, PermissionType, Long> implements PermissionTypeService {

	@Autowired
	private PermissionTypeRepository permissionTypeRepository;

	@Override
	protected AggregateRepository<PermissionType, Long> getAggregateRepository() {
		return this.permissionTypeRepository;
	}

	@Override
	public Long save(PermissionTypeSto permissionTypeSto) {
		PermissionType permissionType = new PermissionType(permissionTypeSto);
		return permissionTypeRepository.add(permissionType);
	}

	@Override
	public void update(PermissionTypeMto permissionTypeMto) {
		ExtAssert.notNull(permissionTypeMto, "对象为空");
		ExtAssert.notNull(permissionTypeMto.getId(), "id为空");
		PermissionType data = permissionTypeRepository.getById(permissionTypeMto.getId());
		data.modify(permissionTypeMto);
		permissionTypeRepository.update(data);
	}

	@Override
	public void delete(Long id) {
		PermissionType permissionType = this.permissionTypeRepository.getById(id);
		if (permissionType == null) {
			return;
		}
		permissionType.destory();
		permissionTypeRepository.removeById(id);
	}

	@Override
	public List<PermissionTypeVo> getAllSystemTypeNames() {
		return permissionTypeRepository.getAllSystemTypeNames().stream().map(model -> {
			PermissionTypeVo permissionTypeVo = new PermissionTypeVo();
			ExtBeanUtils.copyProperties(model, permissionTypeVo);
			return permissionTypeVo;
		}).collect(Collectors.toList());
	}

	@Override
	public void batchSave(Map<Integer, List<PerTypePackageDto>> perTypePackageDtos) {
		perTypePackageDtos.keySet().stream().sorted().forEach(key -> {
			List<PerTypePackageDto> dtos = perTypePackageDtos.get(key);
			dtos.stream().forEach(dto -> {
				if (ExtStringUtils.isBlank(dto.getUuid())) {
					logger.error(dto.getPermissionTypeName() + "无uuid");
					return;
				}
				PermissionType type = permissionTypeRepository.getOne(Wrapper.build().append(Restrictions.eq("uuid", dto.getUuid())));
				PermissionType superType = permissionTypeRepository.getOne(Wrapper.build().append(Restrictions.eq("uuid", dto.getSuperUuid())));
				if (type != null) {
					ExtBeanUtils.copyProperties(dto, type, false);
					//将需要导入的类型状态置为可用状态,预防类型停用后重新启用
					type.setDel(Short.valueOf("1"));
					if ("0".equals(dto.getSuperUuid())) {
						type.setSuperId(0L);
					} else if (superType != null && !superType.getId().equals(type.getSuperId())) {
						type.setSuperId(superType.getId());
					}
					this.saveOrUpdate(type);
				} else {
					//查找是否有匹配模块名
					Wrapper wrapper = Wrapper.build().append(Restrictions.eq("permissionTypeName", dto.getPermissionTypeName()));
					wrapper.append(Restrictions.eq("terminalType", dto.getTerminalType()));
					PermissionType oldType = permissionTypeRepository.getOne(wrapper);
					if (oldType == null) {
						oldType = new PermissionType();
					}
					if (superType != null && !superType.getId().equals(oldType.getSuperId())) {
						oldType.setSuperId(superType.getId());
					} else if ("0".equals(dto.getSuperUuid())) {
						oldType.setSuperId(0L);
					}
					ExtBeanUtils.copyProperties(dto, oldType, false);
					this.saveOrUpdate(oldType);
				}
			});
		});
	}

	@Override
	public void batchDiscardPermissionTypes(List<Long> ids) {
		permissionTypeRepository.batchDiscardPermissionTypes(ids);
	}


	public void saveOrUpdate(PermissionType type) {
		if (type.getId() != null) {
			permissionTypeRepository.update(type);
		} else {
			permissionTypeRepository.add(type);
		}
	}


}
